package org.example.computerdesign.service.imp;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.computerdesign.domain.po.Patient;
import org.example.computerdesign.domain.vo.PatientV;
import org.example.computerdesign.exception.ServiceException;
import org.example.computerdesign.mapper.PatientMapper;
import org.example.computerdesign.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author 朱文权
 * */
@Service
public class PatientServiceImp extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Override
    public void addPatient(String identity, String name, Short sex) {
        Patient patient=patientMapper.selectByIdentity(identity);
        if(patient!=null){
            throw new ServiceException("已添加过该病人",400);
        }
        patient=new Patient();
        patient.setName(name)
                .setSex(sex)
                .setIdentity(identity);
        patientMapper.insert(patient);
    }

    @Override
    public void deletePatient(Long id) {
        Patient patient=patientMapper.selectById(id);
        if(patient==null){
            throw new ServiceException("未查询到该病患",400);
        }
        patientMapper.deleteById(id);
    }

    @Override
    public PatientV getPatientInfo(String identity) {
        Patient patient=patientMapper.selectByIdentity(identity);
        if(patient==null){
            throw new ServiceException("未查询到该病患",400);
        }
        return BeanUtil.copyProperties(patient, PatientV.class);
    }

    @Override
    public IPage<Patient> getAll(Integer p, Integer size) {
        Page<Patient> page=new Page<>(p,size);
        return patientMapper.selectPage(page,null);
    }
}
